package com.yanqu.road.server.logic.activity.condition.normal;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

/**
 * 商会成员同名活动（同心）
 */
public class UnionSameNicknameCountCondition extends BaseActivityCondition {

	public UnionSameNicknameCountCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
		super(activityInfo, activityConditionInfo, baseActivityData);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.UnionSameNickname.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.UnionSameNickname.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
	}

	@Override
	public void initDefaultValue() {
		if(isGetReward()){
			return;
		}
		int memberCount = 0;
		String unionUid = player.getUserInfo().getUnionUid();
		if(!StringUtils.isNullOrEmpty(unionUid)){
			UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
			if(null != unionInfo){
				memberCount = CrossUnionMgr.getSameNicknameCount(unionInfo);
			}
			if(memberCount > 0){
				setValue(BigInteger.valueOf(memberCount));
			}
		}
	}

	@Override
	protected void player_BoardLevelHandle(Object object) {
		int args = (int)object;
		setValue(BigInteger.valueOf(args));
	}
}
